package com.quanquan.management.controller;

import DTO.PageDTO;
import DTO.store.StoreInfoDTO;
import base.BaseConst;
import com.quanquan.management.dto.AdminDTO;
import com.quanquan.management.request.AdminRequest;
import com.quanquan.management.service.AdminService;
import com.quanquan.management.service.StoreService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import web.response.ResultBody;

/**
 * @ClassName:AdminController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/4/15 14:52
 * @Version 1.0
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends ManagerBaseController{

    private final StoreService storeService;
    private final AdminService adminService;

    @Autowired
    public AdminController(AdminService adminService, StoreService storeService) {
        this.adminService = adminService;
        this.storeService = storeService;
    }

    /**
     * 多条件查询 优惠券列表
     *
     * @param pageSize
     * @param page
     * @param keyword
     * @param sort
     * @param isAsc
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) boolean isAsc,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        AdminRequest requestBody = new AdminRequest(page, pageSize, keyword, sort, isAsc);
        PageDTO<AdminDTO> pageDTO = adminService.list(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "admin/list";
    }


    @RequestMapping(value = "/edit")
    public String edit(String id, AdminRequest requestBody, Model model) {
        StoreInfoDTO storeInfoDTO = storeService.list();
        model.addAttribute("storeInfoDTO",storeInfoDTO);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            AdminDTO byId = adminService.findById(id);
            model.addAttribute("editable", byId);
            return "admin/form";
        }
        boolean update = adminService.update(requestBody);
        if (update) {
            return "redirect:/admin/list";
        }
        return null;
    }

    @RequestMapping(value = "/delete")
    public String delete(String id) {
        boolean delete = adminService.delete(id);
        if (delete) {
            return "redirect:/admin/list";
        }
        return null;
    }

    /**
     * 新增
     * @param requestBody
     * @return
     */
    @RequestMapping(value = "/add")
    public String add(AdminRequest requestBody, Model model) {
        StoreInfoDTO storeInfoDTO = storeService.list();
        model.addAttribute("storeInfoDTO",storeInfoDTO);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            return "admin/form";
        }
        boolean add = adminService.add(requestBody);
        if (add) {
            return "redirect:/admin/list";
        }
        return null;
    }

    @GetMapping("/find/{account}")
    public @ResponseBody ResultBody findByName(@PathVariable String account){
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),adminService.findByAccount(account));
    }

}
